package middleware

import (
	"gateway-server/pkg/beego"
)

func CORS() {
	beego.InsertGlobalFilterChain(func(ctx *beego.BeeCtx, next beego.BeeNextFunc) {
		ctx.Output.Header("Access-Control-Allow-Origin", "*")
		ctx.Output.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
		ctx.Output.Header("Access-Control-Allow-Headers", "Content-Type, Authorization")

		// 处理预检请求
		if ctx.Input.Method() == "OPTIONS" {
			ctx.Output.Status = 204
			return
		}
		next(ctx)
	})
}
